<?php
/**
 * @package WordPress
 * @subpackage APrints_Theme
 */

// load jQuery actions
add_action('template_redirect', 'them_js_head_load');
function them_js_head_load(){
	wp_enqueue_script('tagswitch', get_bloginfo('template_directory') . '/scripts/tags.js', array('jquery', 'jquery-ui-core'), '1.0');
	wp_enqueue_script('excerptopener', get_bloginfo('template_directory') . '/scripts/excerpt.js', array('jquery', 'jquery-ui-core'), '1.0');
}
// tag as list
function ap_tag_list(){
	$args=array(
			  'orderby' => 'name',
			  'order' => 'ASC'
			  );
	$tags=get_tags($args);
 	$firstLetter = "";
 	foreach($tags as $tag) :
 		$letter = strtoupper(substr($tag->name,0,1));
 		if($letter != $firstLetter) 
 			echo "<ul><li><h2>".$letter."</h2></li></ul>";
 		echo '<ul class="tagindent"><li><a title="'.$tag->name.'" href="'.get_tag_link( $tag->term_id ).'">'. $tag->name . ' ('.$tag->count.')</a></li></ul>';
	 	$firstLetter=$letter; 
	 endforeach; 
}

// "special" dates
function ap_get_archives(){
	global $wpdb,$wp_locale;
	$defaults = array(
	                'type' => 'monthly', 'limit' => '',
	                'format' => 'html', 'before' => '',
	                'after' => '', 'show_post_count' => true,
	                'echo' => 1
	);
	$r = wp_parse_args( $defaults, $defaults );
	extract( $r, EXTR_SKIP );
	$where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
	$join = apply_filters('getarchives_join', "", $r);
	
	$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date ASC";
	$arcresults = $wpdb->get_results($query);
	if ( $arcresults ) {
		$firstYear = "";
		foreach ( (array) $arcresults as $arcresult ) {
			if($firstYear != $arcresult->year){
				$url = get_year_link($arcresult->year);
				echo "<ul><li><h2><a href='".$url."' title='".$arcresult->year."'>".$arcresult->year."</a></h2></li></ul>";
			}
        	$url = get_month_link( $arcresult->year, $arcresult->month );
            /* translators: 1: month name, 2: 4-digit year */
            $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);
            $url = get_month_link( $arcresult->year, $arcresult->month );
            echo '<ul class="tagindent"><li><a title="'.$text.'" href="'.$url.'">'. $text . ' ('.$arcresult->posts.')</a></li></ul>';

            $firstYear=$arcresult->year; 
		}
	}
	        
}



automatic_feed_links();

if ( function_exists('register_sidebar') ) {
	register_sidebar(array(
		'before_widget' => '<li id="%1$s" class="widget %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h2 class="widgettitle">',
		'after_title' => '</h2>',
	));
}
